package com.wb.common;

import com.wb.util.FileUtil;
import com.wb.util.JsonUtil;
import com.wb.util.StringUtil;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONObject;

public class UrlBuffer {
	public static ConcurrentHashMap<String, String> buffer;
	private static final File file;

	static {
		file = new File(Base.path, "wb/system/url.json");
	}

	public static String get(String url) {
		return (String) buffer.get(url);
	}

	public static void put(String url, String path) {
		buffer.put(url, path);
	}

	public static boolean exists(String url, File file) throws IOException {
		if (!StringUtil.isEmpty(url)) {
			String urlPath = get('/' + url);
			String relPath;
			if (file == null) {
				relPath = null;
			} else {
				relPath = FileUtil.getModulePath(file);
			}

			if (urlPath != null && (relPath == null || !StringUtil.isSame(urlPath, relPath))) {
				return false;
			}
		}

		return true;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean remove(String path) {
		Set<Entry<String, String>> es = buffer.entrySet();
		boolean result = false;
		String delPath = StringUtil.concat(new String[]{path, "/"});
		Iterator var7 = es.iterator();

		while (var7.hasNext()) {
			Entry<String, String> e = (Entry) var7.next();
			String key = (String) e.getKey();
			String modulePath = StringUtil.concat(new String[]{(String) e.getValue(), "/"});
			if (modulePath.startsWith(delPath) && key.length() > 1) {
				buffer.remove(key);
				if (!result) {
					result = true;
				}
			}
		}

		return result;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static synchronized void load() {
		try {
			buffer = new ConcurrentHashMap();
			JSONObject object = JsonUtil.readObject(file);
			Set<Entry<String, Object>> es = object.entrySet();
			Iterator var3 = es.iterator();

			while (var3.hasNext()) {
				Entry<String, Object> e = (Entry) var3.next();
				put((String) e.getKey(), (String) e.getValue());
			}

			String portal = Var.getString("sys.portal");
			if (portal.endsWith(".xwl")) {
				put("/", portal);
			} else {
				put("/", StringUtil.select(new String[]{(String) buffer.get("/" + portal), "index"}));
			}

			put("/m", "");
		} catch (Throwable var4) {
			var4.printStackTrace();
			throw new RuntimeException(var4);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void save() throws Exception {
		Set<Entry<String, String>> es = buffer.entrySet();
		JSONObject object = new JSONObject();
		Iterator var4 = es.iterator();

		while (var4.hasNext()) {
			Entry<String, String> e = (Entry) var4.next();
			String key = (String) e.getKey();
			if (!key.equals("/") && !key.equals("/m")) {
				object.put(key, e.getValue());
			}
		}

		FileUtil.syncSave(file, object.toString());
	}

	@SuppressWarnings("rawtypes")
	public static String find(File file) {
		String path = FileUtil.getModulePath(file);
		if (path == null) {
			return null;
		} else {
			Set<Entry<String, String>> es = buffer.entrySet();
			Iterator var5 = es.iterator();

			String key;
			Entry e;
			do {
				if (!var5.hasNext()) {
					return null;
				}

				e = (Entry) var5.next();
				key = (String) e.getKey();
			} while (!path.equals(e.getValue()) || key.equals("/"));

			return key.substring(1);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static boolean change(String src, String dest, boolean isDir) {
		Set<Entry<String, String>> es = buffer.entrySet();
		int srcLen = src.length() + 1;
		boolean result = false;
		src = src + '/';
		if (isDir) {
			dest = dest + '/';
		}

		Iterator var10 = es.iterator();

		while (var10.hasNext()) {
			Entry<String, String> e = (Entry) var10.next();
			String key = (String) e.getKey();
			String value = (String) e.getValue();
			String path = StringUtil.concat(new String[]{value, "/"});
			if (path.startsWith(src)) {
				if (isDir) {
					buffer.put(key, dest + value.substring(srcLen));
				} else {
					buffer.put(key, dest);
				}

				if (!result) {
					result = true;
				}
			}
		}

		return result;
	}
}